package com.qianguo.sender.api;

import android.content.Context;
import android.support.v4.util.ArrayMap;

import com.eva.androidlibrary.api.CommonApi;
import com.eva.androidlibrary.api.HttpRequestCallback;
import com.eva.androidlibrary.api.entity.UserInfo;
import com.qianguo.sender.api.entity.CatchOrder;
import com.qianguo.sender.api.entity.SenderOrderInfo;
import com.qianguo.sender.api.entity.SenderOrderList;
import com.qianguo.sender.api.entity.SenderUserInfo;

/**
 * 配送员Api
 *
 * @author eva
 * @date 16/4/29
 */
public class SenderApi extends CommonApi {

    private static SenderApi sInstance;

    public SenderApi(Context context) {
        super(context);
    }

    /**
     * 初次使用时调用此方法获取实例
     *
     * @return
     */
    public synchronized static SenderApi getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new SenderApi(context);
        }
        return sInstance;
    }

    /**
     * 派送员登录
     *
     * @param account
     *          账号,服务器会自动根据账号特征区分账号类型：手机账号、邮箱账号、普通账号
     * @param pwd
     *          密码，根据加密算法，把明文转为密文之后传递。
     * @param type
     *          1. 账号＋密码登陆
     *          2. 账号＋短信验证码登陆
     * @return
     */
    public void loginSender(String account, String pwd, int type, HttpRequestCallback<SenderUserInfo> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("account", account);
        params.put("pwd", encryptPassword(pwd));
        params.put("type", String.valueOf(type));
        post(SenderUserInfo.class, hostUrl, "/1.0/user/login", params, callback);
    }

    /**
     * 抢单列表
     *
     * @param token
     * @param page
     * @param perpage
     * @param callback
     */
    public void catchOrders(String token, int page, int perpage, HttpRequestCallback<SenderOrderList> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("page", Integer.toString(page));
        params.put("perpage", Integer.toString(perpage));
        post(SenderOrderList.class, hostUrl, "/1.0/order_courier/receivelist", params, callback);
    }

    /**
     * 抢单接口
     * @param token
     * @param ordersn
     * @param callback
     */
    public void catchOrder(String token, String ordersn, HttpRequestCallback<CatchOrder> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("ordersn", ordersn);
        post(CatchOrder.class, hostUrl, "/1.0/order_courier/qiangdan", params, callback);
    }

    /**
     * 订单详情
     *
     * @param ordersn
     * @param callback
     */
    public void getOrderDetail(String token, String ordersn, HttpRequestCallback<SenderOrderInfo> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("ordersn", ordersn);
        post(SenderOrderInfo.class, hostUrl, "/1.0/order_courier/qiangdanview", params, callback);
    }

    /**
     * 获取处理中的订单列表
     *
     * @param token
     * @param page
     * @param perpage
     * @param callback
     */
    public void getDoingOrders(String token, int page, int perpage, HttpRequestCallback<SenderOrderList> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("page", Integer.toString(page));
        params.put("perpage", Integer.toString(perpage));
        post(SenderOrderList.class, hostUrl, "/1.0/order_courier/deallist", params, callback);
    }

    /**
     * 获取派送员已完成的订单列表
     *
     * @param token
     * @param page
     * @param perpage
     * @param callback
     */
    public void getFinishOrders(String token, int page, int perpage, HttpRequestCallback<SenderOrderList> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("page", Integer.toString(page));
        params.put("perpage", Integer.toString(perpage));
        post(SenderOrderList.class, hostUrl, "/1.0/order_courier/finishlist", params, callback);
    }

    /**
     * 确认送达
     *
     * @param token
     * @param ordersn
     * @param callback
     */
    public void confirmReceive(String token, String ordersn, HttpRequestCallback<SenderOrderInfo> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("ordersn", ordersn);
        post(SenderOrderInfo.class, hostUrl, "/1.0/order_courier/confirm", params, callback);
    }

}
